Peng <- "L:/"; Simon <- "D:/L/" ; port <- "X:/L/"
require(R4MFCL)
require(pkpkg)
place <- Simon ; rm(Simon,Peng,port)
basedir <- ppath(place,"alb/2012/assessment/Setup/")
condordir <- ppath(place,"alb/2012/assessment/Setup/condor.files/")
mufddir <- ppath(place,"alb/2012/assessment/Data_preparation/Mufdager/")
setwd(basedir)

load(ppath(basedir,"storefish.seasonal.RData"))
rundir <- ppath(place,"alb/2012/assessment/Runs/")

base.doitfile <- ppath(basedir,"condor_doitall.alb")
executables <- c("mfclo32","mfclo32.exe","mfclo64","mfclo64.exe","./DLLs_32/msvcp100_32.dll","./DLLs_32/msvcr100_32.dll","./DLLs_64/msvcp100_64.dll","./DLLs_64/msvcr100_64.dll")
executables <- paste("P:/MFCL/current_MFCL/",executables,sep="")
for (i in 1:length(executables)) file.copy(from = executables[i],to = condordir, overwrite = T)

condor_files <- c("mfcl.X86_64.WINDOWS.bat","mfcl.X86_64.LINUX.bat","mfcl.cfg",
                "mfclo32","mfclo32.exe","mfclo64","mfclo64.exe","msvcp100_32.dll","msvcr100_32.dll","msvcp100_64.dll","msvcr100_64.dll")
condor_files <- ppath(condordir,condor_files)

suball <- readLines(ppath(condordir,"condor.sub"))
inifile <- ppath(basedir,"alb.ini")
dir.create(rundir)
seasf.list <- cbind(1:108,c(rep(1:24,each=4),25:28,rep(29:30,each=4)),c(rep(1:4,24),0,0,0,0,rep(1:4,2)))

a <- list.files("./R4MFCL_changed/",full.names = T)
for(f in a) source(f)
source("R support functions.r")
source("drop_relgrps.r")

load("fishsplits.RData")
load("storefish148.RData")
storefish2 <- data.frame(startyr = as.numeric(fishsplit5[,3]),
                         ssn = c(1:4,rep(1:4,each=2),rep(1:4,each=2),rep(1:4,4), # R1
                              rep(1:4),rep(1:4,each=3),rep(1:4,each=3),rep(1:4,4), # R2
                              rep(1:4,each=2),rep(1:4,5), # R3
                              rep(1:4,each=2),rep(1:4,each=2), rep(1:4,each=2),rep(1:4,1),rep(0,4),rep(1:4,2)))

#------------------------------------------------------------

source(paste(basedir,"R run config functions.r",sep="/"))
combo.dir <- ppath(place,"alb/2012/assessment/Combinations2/")
dir.create(combo.dir);


sourcedir <- ppath(rundir,"a0.ref")
combo.frq <- read.frq(ppath(sourcedir,"alb.frq"))
combo.tag <- read.tag(ppath(sourcedir,"alb.tag"))
combo.ini <- read.ini(ppath(sourcedir,"alb.ini"))
a <- readLines(ppath(sourcedir,"condor_doitall.alb"))
a[grep("1 1 5000",a)] <- "  1 1 10000"
a[grep("1 1 3000",a)] <- "  1 1 5000"
combo.doitall <- a
combo.frq$fish <- storefish148
combobase <- list(frq=combo.frq,tag=combo.tag,ini=combo.ini,doitall=combo.doitall)

repo <- read.par(basecase.parfile)
  fem <- c(95.49,96.59,97.69)
  male <- c(100.3,106.23,102.93)
  Ldiff <- (mean(fem[2:3] - fem[1]) + mean(male[2:3] - male[1])) /4
  fem <- c(0.67,0.73,0.94)
  male <- c(0.56,0.57,0.81)
  Kdiff <- (mean(fem[2:3] - fem[1]) + mean(male[2:3] - male[1])) /4
VBstd  <- list(opt=1,startVB=c(repo$Lmin,repo$Lmax,repo$K,repo$growth_vars),start_agepars=repo$gr_offsets,estVB=c(12,13,14,15,16,184),estphase=c(7,7,7,7,7,7)) ;
VBalt <- list(opt=2,startVB=c(repo$Lmin,repo$Lmax + Ldiff,repo$K + Kdiff,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=NA,estphase=NA) ;
VBalt2 <- list(opt=3,startVB=c(repo$Lmin,repo$Lmax + Ldiff/2,repo$K + Kdiff/2,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=NA,estphase=NA) ;
VBalt3 <- list(opt=4,startVB=c(repo$Lmin,repo$Lmax,repo$K + Kdiff,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=NA,estphase=NA) ;
VBalt4 <- list(opt=5,startVB=c(repo$Lmin,repo$Lmax,repo$K + Kdiff/2,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=NA,estphase=NA) ;
VBalt5 <- list(opt=6,startVB=c(repo$Lmin,repo$Lmax,repo$K + Kdiff,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=c(13),estphase=c(7)) ;
VBalt6 <- list(opt=7,startVB=c(repo$Lmin,repo$Lmax,repo$K + Kdiff/2,repo$growth_vars), start_agepars=repo$gr_offsets,estVB=c(13),estphase=c(7)) ;



runnum <- 1
opt <- rep("",12)
opt[seq(1,11,by=2)] <- c("S","_G","_Cr","_M","_Cpue","_EC")
for (newsteep1 in 1:3) {
  opt[2] <- newsteep1
  for (VBopt in list(VBstd,VBalt,VBalt2)) {
    opt[4] <- VBopt$opt
    for (creeprate in c(0,0.5)) {
      opt[6] <- ifelse(creeprate==0,0,1)
      for (newM in 1:3) {
        opt[8] <- newM
          for (CPUEreg in c(0,1,2)) {
            opt[10] <- CPUEreg
            for (earlyCPUE in c(0,1)) {
              opt[12] <- ifelse(earlyCPUE==0,0,1)
              optionstxt <- c("#steep1",newsteep1,"#VB",paste(VBopt$startVB),"#creep",creeprate,"#M",newM,"#CPUE",CPUEreg,"#earlyCPUE",earlyCPUE)
              thisdir <- paste(combo.dir,formatC(runnum,flag="0",width=3),sep="/")
              thisdir <- paste(thisdir,"_",paste(opt,collapse=""),sep="")
              thisrun <- setup.steepness(combobase,c(0.65,0.8,0.95)[newsteep1],add_header=F,gap=0)
              thisrun <- setup.growth(thisrun,VBopt)
              thisrun <- setup.effcreep(thisrun,creeprate)
              thisrun <- setup.M(thisrun,c(0.3,0.4,0.5)[newM])
              thisrun <- setup.CPUEreg(thisrun,CPUEreg)
              thisrun <- setup.earlyCPUE(thisrun,earlyCPUE)
              dir.create(thisdir)
              write(optionstxt,paste(thisdir,"options.txt",sep="/"))
              condor.go(thisdir,thisrun$frq,thisrun$tag,thisrun$doitall,thisrun$ini,sub.obj=suball,species="alb",condor_files,par.obj=NA,run_now=T)
              runnum <- runnum + 1
  } } } } } }
  
runnum <- 325
opt <- rep("",12)
opt[seq(1,11,by=2)] <- c("S","_G","_Cr","_M","_Cpue","_EC")
for (newsteep1 in 1:3) {
  opt[2] <- newsteep1
  for (VBopt in list(VBalt3,VBalt4,VBalt5,VBalt6)) {
    opt[4] <- VBopt$opt
    for (creeprate in c(0,0.5)) {
      opt[6] <- ifelse(creeprate==0,0,1)
      for (newM in 1:3) {
        opt[8] <- newM
          for (CPUEreg in c(0,1,2)) {
            opt[10] <- CPUEreg
            for (earlyCPUE in c(0,1)) {
              opt[12] <- ifelse(earlyCPUE==0,0,1)
              optionstxt <- c("#steep1",newsteep1,"#VB",paste(VBopt$startVB),"#creep",creeprate,"#M",newM,"#CPUE",CPUEreg,"#earlyCPUE",earlyCPUE)
              thisdir <- paste(combo.dir,formatC(runnum,flag="0",width=3),sep="/")
              thisdir <- paste(thisdir,"_",paste(opt,collapse=""),sep="")
              thisrun <- setup.steepness(combobase,c(0.65,0.8,0.95)[newsteep1],add_header=F,gap=0)
              thisrun <- setup.growth(thisrun,VBopt)
              thisrun <- setup.effcreep(thisrun,creeprate)
              thisrun <- setup.M(thisrun,c(0.3,0.4,0.5)[newM])
              thisrun <- setup.CPUEreg(thisrun,CPUEreg)
              thisrun <- setup.earlyCPUE(thisrun,earlyCPUE)
              dir.create(thisdir)
              write(optionstxt,paste(thisdir,"options.txt",sep="/"))
              condor.go(thisdir,thisrun$frq,thisrun$tag,thisrun$doitall,thisrun$ini,sub.obj=suball,species="alb",condor_files,par.obj=NA,run_now=T)
              runnum <- runnum + 1
  } } } } } }
  
 
